package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.ShopOrderListResponse
import com.gexiaobao.pigeon.databinding.ItemGoodsMineOrderBinding

/**
 * @author: hxw
 * @date  : 2023/2/2
 * @desc  :
 */
class GoodsItemAdapter(data: ArrayList<ShopOrderListResponse.ListBean.ListStore>, dataShop: ShopOrderListResponse.ListBean) :
    BaseQuickAdapter<ShopOrderListResponse.ListBean.ListStore, DataBindBaseViewHolder>(R.layout.item_goods_mine_order, data) {

    private var mOnItemGoodsClickListener: OnChildItemGoodsClickListener? = null
    private var mDataShop: ShopOrderListResponse.ListBean

    init {
        mDataShop = dataShop
    }

    @SuppressLint("SetTextI18n", "NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: ShopOrderListResponse.ListBean.ListStore) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemGoodsMineOrderBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        holder.itemView.apply {
            binding.rvItemGoods.adapter = null
            if (item.isEmpty() || item.list.size == 0) {
                holder.setGone(R.id.itemRecyclerView, true)
                return
            }
            holder.setGone(R.id.rvItemGoods, false)

            /** 设置加载的布局类型 */
            item.list.forEachIndexed { _, listGoods ->
                if (mDataShop.list.size == 1) {
                    listGoods.itemType = 1
                } else {
                    listGoods.itemType = 0
                }
            }

            val mAdapterGoodsOrder = GoodsOrderItemAdapter(item.list)
            binding.rvItemGoods.apply {
                layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
                adapter = mAdapterGoodsOrder
                mAdapterGoodsOrder.notifyDataSetChanged()
            }

            mAdapterGoodsOrder.setOnItemClickListener { _, _, position ->
                mOnItemGoodsClickListener?.onChildItemGoodsClick(position)
            }
        }
    }

    fun setOnChildGoodsItemClickListener(listener: OnChildItemGoodsClickListener?) {
        mOnItemGoodsClickListener = listener
    }

    interface OnChildItemGoodsClickListener {
        fun onChildItemGoodsClick(position: Int)
    }
}